﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using SundStudent.ModelLayer;

namespace SundStudent.DBLayer
{
   public  class DBLektionsPatient
    {
        private SqlCommand dbCmd = null;

        public LektionsPatient FindPatient(string navn)
        {
            string sql = @"select nr, patientnr, navn, adresse, postnr, byen, telefon, alder, dato,famorienteret, medicin, vaerdi from Lektionspatient " +
                           " where navn  = '" + navn + "'";

            Console.WriteLine("sql stmt = " + sql);

            dbCmd = DBConnection.GetDbCommand(sql);

            IDataReader dbReader;
            dbReader = dbCmd.ExecuteReader();

            LektionsPatient patientObj;

            if (dbReader.Read())
            {
                try
                {
                    Console.WriteLine("antal felter " + dbReader.FieldCount);
                    patientObj = buildLektionsPatientObj(dbReader);

                }
                catch (Exception e)
                {
                    Console.WriteLine("Lektions patient objekt ikke oprettet korrekt");
                    patientObj = null;
                }


            }

            else
            {
                patientObj = null;
            }
            dbReader.Close();
            DBConnection.Close();
            return patientObj;
        }
        public LektionsPatient FindLektionsPatient(int lektionsid)
        {
            string sql = @"select nr, patientnr, navn, adresse, postnr, byen, telefon, alder, dato,famorienteret, medicin, vaerdi,lektionsdato, lektionsid, seng, stue, modulnr from Lektionspatient " +
                           " where lektionsid  = " + lektionsid;

            Console.WriteLine("sql stmt = " + sql);

            dbCmd = DBConnection.GetDbCommand(sql);

            IDataReader dbReader;
            dbReader = dbCmd.ExecuteReader();

            LektionsPatient patientObj;

            if (dbReader.Read())
            {
                try
                {
                    Console.WriteLine("antal felter " + dbReader.FieldCount);
                    patientObj = buildLektionsPatientObj(dbReader);

                }
                catch (Exception e)
                {
                    Console.WriteLine("Lektions patient objekt ikke oprettet korrekt");
                    patientObj = null;
                }


            }

            else
            {
                patientObj = null;
            }
            dbReader.Close();
            DBConnection.Close();
            return patientObj;
        }

        public List<LektionsPatient> HentAllePatienter()
        {
            List<LektionsPatient> returnList = new List<LektionsPatient>();
            string sql = @"select nr, patientnr, navn, adresse, postnr, byen, telefon, alder, dato,famorienteret, medicin, vaerdi,lektionsdato, lektionsid, seng, stue, modulnr from lektionspatient ";


            Console.WriteLine("sql stmt = " + sql);

            dbCmd = DBConnection.GetDbCommand(sql);

            IDataReader dbReader;
            dbReader = dbCmd.ExecuteReader();

            LektionsPatient patientObj;

            while (dbReader.Read())
            {

                patientObj = buildLektionsPatientObj(dbReader);
                returnList.Add(patientObj);
            }
            DBConnection.Close();
            return returnList;

        }
        private LektionsPatient buildLektionsPatientObj(IDataReader dbReader)
        {
            LektionsPatient patientObj = new LektionsPatient();
            try
            {
                Console.WriteLine("antal felter " + dbReader.FieldCount);
                patientObj.Nr = Convert.ToInt32(dbReader["nr"].ToString());
                patientObj.Patientnt = Convert.ToInt32(dbReader["patientnr"].ToString());
                patientObj.Navn = dbReader["navn"].ToString();
                patientObj.Adresse = dbReader["adresse"].ToString();
                patientObj.Postnr = Convert.ToInt32(dbReader["postnr"].ToString());
                patientObj.Byen = dbReader["byen"].ToString();
                patientObj.Telefon = Convert.ToInt32(dbReader["telefon"].ToString());
                patientObj.Alder = Convert.ToInt32(dbReader["alder"].ToString());
                patientObj.Medicin = dbReader["medicin"].ToString();
                patientObj.Samtykke = dbReader["famorienteret"].ToString();
                patientObj.Vaerdi = dbReader["vaerdi"].ToString();
                patientObj.IndDato = Convert.ToDateTime(dbReader["dato"].ToString());
                Modul modulObj = new Modul();
                patientObj.Modul = modulObj;
                patientObj.Modul.Modulnr = Convert.ToInt32(dbReader["modulnr"].ToString());
                Seng sengObj = new Seng();
                sengObj.Sengnr = Convert.ToInt32(dbReader["seng"].ToString());
                patientObj.Seng = sengObj;
                Stue stueObj = new Stue();
                stueObj.Stuenr = dbReader["stue"].ToString();
                patientObj.Stue = stueObj;

            }
            catch (Exception ex)
            {

                Console.WriteLine("Lektions patient objekt ikke oprettet korrekt");
                Console.WriteLine(ex);
                patientObj = null;
            }

            return patientObj;
        }
        public int InsertLektionsPatient(LektionsPatient patientObj, Lektion lektionObj, Seng sengObj)
        {
            int rc = -1;
            SqlCommand dbCmd = null;
            try
            { // tildel naeste nr
                patientObj.Nr = 1;
                string sql = "select max(nr) as nextid from lektionsPatient";
                SqlCommand dbCmd1 = DBConnection.GetDbCommand(sql);
                Console.WriteLine("maxid lektionspatient " + sql);
                IDataReader dbReader;
                dbReader = dbCmd1.ExecuteReader();
                if (dbReader.Read())
                {
                    try
                    {
                        patientObj.Nr = Convert.ToInt32(dbReader["nextid"].ToString()) + 1;
                        Console.WriteLine("næstenr lektionsid: " + patientObj.Nr);
                    }
                    catch (Exception exobj)
                    {
                        patientObj.Nr = 1;
                    }
                }
            
                DBConnection.Close();
               


                string sql3 = " insert into LektionsPatient ( nr, patientnr, navn, adresse, postnr, byen, telefon, alder,dato," 
                              + "famorienteret, medicin, vaerdi, seng, stue, lektionsdato, lektionsid, modulnr) " +
                              " values (" + patientObj.Nr + ","
                                + patientObj.Patientnt + ",'"
                                + patientObj.Navn + "','"
                                + patientObj.Adresse + "',"
                                + patientObj.Postnr + ",'"
                                + patientObj.Byen + "',"
                                + patientObj.Telefon + ","
                                + patientObj.Alder + ",'"
                                + datoOmForm(patientObj.IndDato)+ "','"
                                + patientObj.Samtykke + "','"
                                + patientObj.Medicin + "','"
                                + patientObj.Vaerdi + "',"
                                + sengObj.Sengnr + ",'"
                                + sengObj.Stue.Stuenr  + "','"
                                + datoOmForm(lektionObj.Dato) + "',"
                                + lektionObj.Nr  + ","
                                + lektionObj.ModulObj.Modulnr 
                                + ")";


                Console.WriteLine("Insert sql : " + sql3);
                dbCmd = DBConnection.GetDbCommand(sql3);

                rc = dbCmd.ExecuteNonQuery();
                DBConnection.Close();



            }
            catch (Exception insertE)
            {
                Console.WriteLine("Fejl i insert Lektions patient");
                Console.WriteLine(insertE);
            }
            DBConnection.Close();
            return rc;
        }
        private String datoOmForm(DateTime dato)
        {

            String myDate = dato.Year.ToString();
            if (dato.Month < 10)
            {
                myDate = myDate + "0" + dato.Month;
            }
            else
            {
                myDate = myDate + dato.Month;
            }
            if (dato.Day < 10)
            {
                myDate = myDate + "0" + dato.Day;
            }
            else
            {
                myDate = myDate + dato.Day;
            }
            Console.WriteLine("Dato to String " + myDate);
            return myDate;
        }
    }
}
